<template>
  <view class="page-wraper flex-col">
    <!-- {{ token }} {{ jobList.length }}  {{ loading }}  {{txt}} --> 
    <view class="flex-1 hidden bg-gray flex-col page-wraper-content relative" v-if="token">
      <pk-scroll height="0px" @lower="lower" :loading="loading" @refresh="refresh">
        <view  class="page-wraper-body">
          <view v-for="(item, index) in jobList" :key="index" class="mb-24">
            <job-item :data="item" @onEmit="onEmit" :list="resumeList"></job-item>
          </view>
        </view>
      </pk-scroll>
      <pk-emtry title="暂无面试记录" v-if="jobList.length < 1 && !loading"></pk-emtry>
    </view>
    <view class="flex-1 relative flex-col" v-if="!token && !loading">
      <pk-authuser @onLogin="onLogin"></pk-authuser>
    </view>
    <!-- <view class="page-wraper-footer flex-row">
      <view class="page-wraper-footeritem f-12 flex-1 text-center">我的面试</view>
      <view class="page-wraper-footeritem f-12 flex-1 text-center" @click="myresume">我的简历</view>
    </view> -->
  </view>
</template>

<script setup>
  import { ref } from 'vue';
  import { onShow, onShareAppMessage, onShareTimeline, onLoad, onPullDownRefresh } from '@dcloudio/uni-app'
  import jobItem from './component/jobitem.vue'
  import { invitationGetPageList, attachmentGetList, getUserInfo } from '@/api/api'
  import { getTimeYearStr } from '@/utils/utils'
  import WXLOG from '@/utils/log'
  const jobList = ref([])
  const total = ref(0)
  const page = ref(1)
  const pageSize = ref(12)
  const resumeList = ref([])
  const token = ref('')
  const loading = ref(false)
  const txt = ref('')


  onShareAppMessage(() => {
    return {
      title: '我的面试',
      path: 'pages/interview/index', 
    }
  })

  onShareTimeline(() => {
    return {
      title: '我的面试',
      path: 'pages/interview/index', 
    }
  })

  const getList = () => {
    txt.value = 'getlist'
    loading.value = true
    invitationGetPageList({
      page: page.value
    }).then(res => {
      if (res.code == 1000) { 
        jobList.value = jobList.value.concat(res.data.list.map(item => {
          return {
            ...item,
            deadlineCn: getTimeYearStr(item.deadline, 'time')
          }
        }))
        total.value = res.data.total 
      }
      if (res.code == 1008) {
        token.value = ''
      }
      loading.value = false
    }).catch(err => {
      loading.value = false
      if (err && err.data && err.code == 1008) {

      }
    })
  }

  const onEmit = () => {
    page.value = 1
    jobList.value = []
    getList()
  }

  const lower = () => {
    if (total.value > jobList.value.length) {
      page.value = page.value + 1
      getList()
    }
    console.log('-------lower--------')
  }

  const myresume = () => {
    uni.navigateTo({
      url: '/pages/resume/index'
    })
  }

  const getResumeList = () => {
    attachmentGetList().then(res => {
      resumeList.value = res.data.map(item => {
        return {
          ...item,
          createdAt: getTimeYearStr(item.created_at, 'time')
        }
      })
      console.log('----------------', res)
    })
  }

  const onLogin = () => {
    txt.value = '授权成功'
    token.value = uni.getStorageSync('bytesbayToken') || ''
    page.value = 1
    jobList.value = []
    getList()
    getResumeList()
  }

  onLoad(() => { 
    txt.value = '进入'
    token.value = uni.getStorageSync('bytesbayToken') || ''
    WXLOG.info('info', '-------我的面试-----------', uni.getStorageSync('bytesbayToken'))
    if (uni.getStorageSync('bytesbayToken')) {
      onLogin()
    } 
    uni.startPullDownRefresh();
  })

  const refresh = () => {
    token.value = uni.getStorageSync('bytesbayToken') || ''
    page.value = 1
    jobList.value = []
    getList()
    getResumeList()
  }


  // refresh(() => {
  //   token.value = uni.getStorageSync('bytesbayToken') || ''
  //   page.value = 1
  //   jobList.value = []
  //   getList()
  //   getResumeList()
  //   setTimeout(function () {
	// 		uni.stopPullDownRefresh();
	// 	}, 1000);
  // })

  onShow(() => {
    token.value = uni.getStorageSync('bytesbayToken') || ''
  })
</script>

<style lang="scss" scoped>
.page-wraper{
  &-content{
    overflow: hidden;
  }
  &-body{
    padding: 24rpx;
    // padding-bottom: 100rpx;
    // padding: 24rpx 24rpx 100rpx 24rpx;
  }
  &-footer {
    padding-bottom: 24rpx;
    background: #00b4b3;
  }
  &-footeritem{
    height: 80rpx;
    line-height: 80rpx;
    background: #00b4b3;
    color: #fff;
  }
}
</style>